﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebBrowserTest.AnalyzeResults;

namespace WebBrowserTest.Analyzers
{
    public class GoogleCacheAnalyzer : AbstractAnalyzer
    {
        public string LookupExpression
        {
            get { return _lookupExpr; }
            set { _lookupExpr = value; }
        }

        public GoogleCacheAnalyzer()
        {
            _lookupExpr = @"as it appeared on (\d{1,2} [a-zA-Z]{3} \d{4}) \d{2}:\d{2}:\d{2} GMT\.|Your search - <b>[^<]+<\/b> - did (not match) any documents";
        }
        public GoogleCacheAnalyzer(string lookupExpr)
        {
            this.LookupExpression = lookupExpr;
        }

        public override AbstractAnalyzeResult GetAnalyseResult(string html, string url)
        {
            var matches = Regex.Matches(html, LookupExpression);
            var backlinkCounter = 0;
            var cacheDate = new DateTime();
            for (var i = 0; i < matches.Count; i++)
            {
                var cacheDateString = matches[i].Groups[1].Value;
                if (!string.IsNullOrEmpty(cacheDateString.Trim()))
                {
                    cacheDate = DateTime.Parse(cacheDateString);
                }
                break;
            }
            return new GoogleCacheResult() { CacheDate = cacheDate, DomainName = url, Date = DateTime.Now };
        }
    }
}
